package com.android.plc.ui.manager;

import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;

import com.android.plc.R;
import com.android.plc.classholder.BasicItem;
import com.android.plc.classholder.BasicItem.Type;
import com.android.plc.classholder.Device;
import com.android.plc.classholder.Room;
import com.android.plc.provider.PLC;
import com.android.plc.provider.PLC.Devices;
import com.android.plc.ui.dialog.DeviceCodeDialog;
import com.android.plc.ui.dialog.DeviceTypeDialog;
import com.android.plc.ui.dialog.OnWheelSelectListener;
import com.android.plc.util.StringUtil;

public class DeviceManager extends BaseManager<Device>
{
	private Button mOKBtn;
	private Button mDelBtn;
	private EditText mTxtName;
	private EditText mTxtDeviceType;
	private EditText mTxtDeviceCode;
	private int mType = 0;
	private int mSubType = 0;
	private int mCode = 0;

	public DeviceManager(Context context, ViewGroup view)
	{
		super(context, view, R.layout.add_device);
	}

	@Override
	public void initView(ViewGroup controlView)
	{
		mOKBtn = (Button) controlView.findViewById(R.id.edit_save);
		mDelBtn = (Button) controlView.findViewById(R.id.edit_del);
		mTxtName = (EditText) controlView.findViewById(R.id.editDeviceName);
		mTxtDeviceType = (EditText) controlView.findViewById(R.id.edit_device_type);
		mTxtDeviceCode = (EditText) controlView.findViewById(R.id.edit_device_code);

		setDeviceType(0, 0);
		mTxtDeviceCode.setClickable(true);
		mTxtDeviceCode.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				showCodeDialog();
			}
		});
		mTxtDeviceCode.setOnFocusChangeListener(new OnFocusChangeListener()
		{

			public void onFocusChange(View v, boolean hasFocus)
			{
				if (hasFocus)
				{
					showCodeDialog();
				}
			}
		});
		setDeviceCode(0);
		mTxtDeviceType.setClickable(true);
		mTxtDeviceType.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				showTypeDialog();
			}
		});
		mTxtDeviceType.setOnFocusChangeListener(new OnFocusChangeListener()
		{

			public void onFocusChange(View v, boolean hasFocus)
			{
				if (hasFocus)
					showTypeDialog();
			}
		});
		mOKBtn.setOnClickListener(this);
		mDelBtn.setOnClickListener(this);
		mTxtName.addTextChangedListener(this);
	}

	private void showTypeDialog()
	{
		DeviceTypeDialog dialog = new DeviceTypeDialog(mContext);
		dialog.setSelectCodeListener(new OnWheelSelectListener()
		{
			public void onSelected(Object obj)
			{

				int result = (Integer) obj;
				int device_type = result >> 4;
				int sub_type = result & 0x0F;
				setDeviceType(device_type, sub_type);
				Device device = getDevice();

				if (device != null && device._id > 0 && device.device_type != result)
				{
					device.device_type = device_type;
					device.sub_type = sub_type;
					update(device);
				}
			}
		});
		dialog.show();
	}

	private void showCodeDialog()
	{
		Device device = getDevice();
		DeviceCodeDialog dialog = new DeviceCodeDialog(mContext);
		dialog.setSelectCodeListener(new OnWheelSelectListener()
		{
			public void onSelected(Object obj)
			{
				int result = (Integer) obj;
				Device device = getDevice();
				if (device != null && device._id > 0 && device.code != result)
				{
					device.code = result;
					update(device);
				}
				setDeviceCode(result);
			}
		});
		dialog.show();
		dialog.selectDeviceCode(device.code);
	}

	public Uri getUri()
	{
		return PLC.Devices.CONTENT_URI;
	}

	@Override
	public ContentValues item2maps(Device device)
	{
		ContentValues values = new ContentValues();
		values.put(Devices.NAME, device.name);
		values.put(Devices.TYPE, device.device_type);
		values.put(Devices.SUB_TYPE, device.sub_type);
		values.put(Devices.ROOM_ID, device.room);
		values.put(Devices.CODE, device.code);
		return values;
	}

	public void onClick(View arg0)
	{
		final Device device = getDevice();
		switch (arg0.getId())
		{
		case R.id.edit_save:
			Room room = getRoom();
			if (checkValidity(getView()) && room != null)
			{
				device.name = mTxtName.getText().toString().trim();
				device.code = mCode;
				device.device_type = mType;
				device.sub_type = mSubType;
				device.room = room._id;
				insert(device);
			}
			break;
		case R.id.edit_del:
			if (device != null)
			{
				delete(device);
			}
			break;
		default:
			break;
		}

	}

	@Override
	public boolean checkValidity(View view)
	{
		if (mTxtName == null)
			return false;
		if (mTxtName.getText().toString().trim().length() == 0)
		{
			return false;
		}
		return true;
	}

	@Override
	public void displayItem(Device item)
	{
		if (item == null)
		{
			return;
		}
		if (item.type == Type.ADD_ITEM)
		{
			mTxtName.setText("");
			setDeviceCode(0);
			setDeviceType(0, 0);
			mOKBtn.setVisibility(View.VISIBLE);
			mDelBtn.setVisibility(View.GONE);
		} else if (item.type == Type.DEL_ITEM)
		{
			mOKBtn.setVisibility(View.GONE);
			mDelBtn.setVisibility(View.VISIBLE);
		} else if (item.type == Type.NORMAL_ITEM)
		{
			mTxtName.setText(item.name);
			setDeviceCode(item.code);
			setDeviceType(item.device_type, item.sub_type);

			mOKBtn.setVisibility(View.GONE);
			mDelBtn.setVisibility(View.VISIBLE);
		}
	}

	public void afterTextChanged(Editable s)
	{
		Device device = getDevice();
		String deviceName = mTxtName.getText().toString().trim();
		boolean same = deviceName.equals(device.name);
		if (device != null && device._id > 0 && checkValidity(getView()) && !same)
		{
			device.name = deviceName;
			update(device);
		}
	}

	public void onBasicItemSelected(Manager mgr, BasicItem item, boolean admin)
	{
		Device device = getDevice();
		if (mgr == Manager.ROOM)
		{
			device.room = ((Room) item)._id;
		}
		displayItem(device);
	}

	private void setDeviceCode(int code)
	{
		mCode = code;
		mTxtDeviceCode.setText(StringUtil.convDeviceCode(mContext, code));
	}

	private void setDeviceType(int type, int subType)
	{
		mType = type;
		mSubType = subType;
		mTxtDeviceType.setText(StringUtil.convDeviceType(mContext, mType, mSubType));
	}
}
